import { Matrix, Vector3, Vector4 } from "@babylonjs/core";

function applyMatrix4(v: Vector3, m: Matrix) {
    const x = v.x, y = v.y, z = v.z;
    const e = m.asArray();
    const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
    v.x = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w;
    v.y = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w;
    v.z = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w;
    return v;
}

function getDefaultMatrix() {
    const m1 = new Matrix();
    m1.setRow(0, new Vector4(1, 0, 0, 0));
    m1.setRow(1, new Vector4(0, 1, 0, 0));
    m1.setRow(2, new Vector4(0, 0, 1, 0));
    m1.setRow(3, new Vector4(0, 0, 0, 1));
    return m1;
}

export {
    applyMatrix4,
    getDefaultMatrix,
}